package com.haha.project0301.controller;

import com.haha.project0301.dao.DepartmentDao;
import com.haha.project0301.pojo.Department;
import com.haha.project0301.pojo.Employee;
import com.haha.project0301.service.EmpService;
import com.haha.project0301.service.impl.EmpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
 * @author dsx
 * @date 2021/3/1 17:06
 * @since JDK1.8
 */
@Controller
public class EmpController {
    @Autowired
    EmpService empServiceImpl;
    @Autowired
    DepartmentDao departmentDao;
    
    @RequestMapping("/emps")
    public String getAllEmp(Model model){
        Collection<Employee> allEmp = empServiceImpl.getAllEmp();
        model.addAttribute("emps",allEmp);
        System.err.println("进入EmpController==》getAllEmp");
        return "emp/list";
    }
    //跳转到添加员工界面
    @GetMapping("/add")
    public String add(Model model) {
        //查出所有的部门信息,添加到departments中,用于前端接收
        Collection<Department> departments = departmentDao.getAllDepartment();
        for (Department department : departments) {
            System.out.println(department.getDid()+"=="+department.getDname());
        }
        model.addAttribute("departments", departments);
        return "emp/add";//返回到添加员工页面
    }
    //添加员工
    @PostMapping("/add")
    public String add(Employee employee, HttpServletRequest request){
        System.err.println(employee.getDepartment().getDid());//为什么一直返回的都是0
        System.err.println(request.getParameter("department.id"));
        employee.getDepartment().setDid(Integer.parseInt(request.getParameter("department.id")));
        empServiceImpl.addEmp(employee);
        return "redirect:/emps";
    }
    
    @RequestMapping("/update/{id}")
    public String toUpdate(@PathVariable("id")int id,Model model){
//        Employee empById = empServiceImpl.getEmpById(id);
//        System.out.println(empById.toString());
//        model.addAttribute("empById",empById);
//        //查出所有的部门信息,添加到departments中,用于前端接收
//        Collection<Department> departments = departmentDao.getAllDepartment();
//        model.addAttribute("departments", departments);
        //restful风格接收参数
        
        //查询指定id的员工,添加到empByID中,用于前端接收
        Employee employeeByID = empServiceImpl.getEmpById(id);
        model.addAttribute("empByID", employeeByID);
        //查出所有的部门信息,添加到departments中,用于前端接收
        Collection<Department> departments = departmentDao.getAllDepartment();
        model.addAttribute("departments", departments);
        return "emp/update";
    }
    @PostMapping("/update")//使用Map的add请求实现的改，
    public String updated(Employee employee,HttpServletRequest request){
        String id = request.getParameter("department.id");
        employee.getDepartment().setDid(Integer.parseInt(id));
        empServiceImpl.addEmp(employee);
        return "redirect:/emps";
    }
    
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id){
        System.err.println(id);
        empServiceImpl.deleteEmployeeByID(id);
        return "redirect:/emps";
    }
}
